<?php

namespace App\Kuafu\ProductMenu\Validate;

use App\Kuafu\Product\Model\Product;
use App\Kuafu\Shop\Model\Shop;
use Illuminate\Http\Request;
use App\Kuafu\Base\Validate\BaseValidate;

class ProductMenuValidate extends BaseValidate
{
    public static function addMenu($data)
    {
//        $productList = [
//            [
//                'productId' => 1,
//                'isHot' => 1,
//                'isRecommend' => 0,
//            ],
//        ];

        $rules = [
            'name'        => 'required',
            'productList' => [
                'required',
                'array',
                function ($attribute, $value, $fail) use ($data) {
                    foreach ($data['productList'] as $v) {
                        if (!isset($v['productId']) || !isset($v['isHot']) || !isset($v['isRecommend'])) {
                            $fail('productList参数错误');
                        }

                        if (!is_int($v['productId'])) {
                            $fail('productList.productId必须为int');
                        }

                        if (!is_int($v['price'])) {
                            $fail('productList.price必须为int');
                        }

                        if (!in_array($v['isHot'], [0, 1])) {
                            $fail('productList.isHot错误');
                        }

                        if (!in_array($v['isRecommend'], [0, 1])) {
                            $fail('productList.isRecommend');
                        }
//
//                        if (!is_int($v['type'])) {
//                            $fail('productList.type必须为int');
//                        }
                    }
                }
            ],
            'categoryList' => [
                'required',
                'array',
                function ($attribute, $value, $fail) use ($data) {
                    foreach ($data['categoryList'] as $v) {
                        if (isset($v['categoryId']) && !is_int($v['categoryId'])) {
                            $fail('categoryList.categoryId必须为int');
                        }

                        if (isset($v['name']) && !$v['name']) {
                            $fail('categoryList.name不能为空');
                        }
                    }
                }
            ],
        ];

        $messages = [];
        return self::validateCheck($data, $rules, $messages);
    }

    public static function getMenu($data)
    {
        $rules = [
            'pageSize' => self::commonRules('pageSize'),
            'pageNum'  => self::commonRules('pageNum'),
        ];

        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function setMenu($data)
    {
        $rules = [
            'menuId'      => 'required|integer|min:1',
            'name'        => 'required',
            'productList' => [
                'required',
                'array',
                function ($attribute, $value, $fail) use ($data) {
                    foreach ($data['productList'] as $v) {
                        if (!isset($v['productId']) || !isset($v['isHot']) || !isset($v['isRecommend'])) {
                            $fail('productList参数错误');
                        }

                        if (!is_int($v['productId'])) {
                            $fail('productList.productId必须为int');
                        }

                        if (!is_int($v['price'])) {
                            $fail('productList.price必须为int');
                        }

                        if (!in_array($v['isHot'], [0, 1])) {
                            $fail('productList.isHot错误');
                        }

                        if (!in_array($v['isRecommend'], [0, 1])) {
                            $fail('productList.isRecommend');
                        }
                    }
                }
            ],
            'categoryList' => [
                'required',
                'array',
                function ($attribute, $value, $fail) use ($data) {
                    foreach ($data['categoryList'] as $v) {
                        if (isset($v['categoryId']) && !is_int($v['categoryId'])) {
                            $fail('categoryList.categoryId必须为int');
                        }

                        if (isset($v['name']) && !$v['name']) {
                            $fail('categoryList.name不能为空');
                        }
                    }
                }
            ],
        ];

        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function setMenuStatus($data)
    {
        $rules = [
            'productMenuId' => 'required|integer|min:1',
            'status'        => 'required|integer|between:0,2',
        ];

        $messages = [];

        return self::validateCheck($data, $rules, $messages);
    }
}
